-- region *.lua
-- Date
-- 此文件由[BabeLua]插件自动生成

PopUpArgs = {
    _Title; --标题
    _Content; --类容
    
    _CancleBtnText = nil;
    _OkBtnText = nil;
    _OkBigBtnText = nil;

    _OkBigBtnID = nil;
    _CancleBtnID = nil;
    _OkBtnID = nil;

    _CommonHandlerCaller; --Caller
    _CommonHandler; --CallBack
    _CommonHandlerArgs; -- CallBackArgs
    _CloseTime; --关闭时间
    _AutoCloseHandler;
}

function PopUpArgs.Create(args)
    local popUpArgs = New(PopUpArgs);
    popUpArgs._Title  = args[1];
    popUpArgs._Content = args[2];

    local table = args[3];
    popUpArgs._CommonHandlerCaller =args[4];
    popUpArgs._CommonHandler =args[5];
    popUpArgs._CommonHandlerArgs =args[6];
    popUpArgs._CloseTime =args[7]; 
    popUpArgs._AutoCloseHandler =args[8];

    if #table == 1 then
        local btnTable = table[1];
        popUpArgs._OkBigBtnID = btnTable.id;
        popUpArgs._OkBigBtnText = btnTable.text;
    else
        local cancleBtnTable = table[1];
        local okBtnTable = table[2];
        popUpArgs._CancleBtnID = cancleBtnTable.id;
        popUpArgs._OkBtnID = okBtnTable.id;
        popUpArgs._CancleBtnText = cancleBtnTable.text;
        popUpArgs._OkBtnText = okBtnTable.text;
    end
    return popUpArgs;
end 

PopUpDlg = Dialog:DeriveClass("PopUpDlg", "PopUpDlg.prefab", 99,
{
    _TitleText = nil;
    _ContentText = nil;
    _OkBtn = nil;
    _CancleBtn = nil;
    _OkBigBtn = nil;

    OK_BTN_ID = 1;
    CANCLE_BTN_ID = 2;
    OK_BIG_BTN_ID = 3;
    Ok_Btn_Args = { { id = 3, text = "确定" } };
    Cancle_And_Ok_Btn_Args = { { id = 2, text = "取消" }, { id = 1, text = "确定" } };

    _CloseTimer;
    sceneChangeClose = false; --切换场景关闭弹窗 默认false 不关闭
    showBg = false;
    _CloseBtn;

    _ContentHashCode; -- content HashCode
    _ContentHashCodeMap = Map:new(); --保证 同样content的PopUpDlg 只会存在一份

    _dlgArgs;
} )

function PopUpDlg:OnInit()
    self._TitleText = self:GetYLabel("Title");
    self._ContentText = self:GetYLabel("Content");
    self._OkBtn = self:GetYButton("OkBtn");
    self._CancleBtn = self:GetYButton("CancleBtn");
    self._OkBigBtn = self:GetYButton("OkBigBtn");
    self._bgImage = self:GetWindow().Root:GetComponent("YImage");
    self._bgImage.enabled = false;
    self._CloseBtn = self:GetYButton("sys_CloseButton")
end

-- 使用说明
-- PopUpDlg.PopUpCommonHandler(title,content,{ { id = CANCLE_BTN_ID, text = "取消" }, { id = OK_BTN_ID, text = "确定" } },CommHandlerCaller,CommHandler,CommHandlerArgs,CloseTimer)
-- 提供了2个公用的 btn Tabel PopUpDlg.Ok_Btn_Args PopUpDlg.Cancle_And_Ok_Btn_Args 回掉函数会把BtnID 传回去
-- 例子 PopUpDlg.PopUpCommonHandler("提示","是否升级该技能?",PopUpDlg.Cancle_And_Ok_Btn_Args,caller,function,functionArgsTable);
function PopUpDlg.PopUpCommonHandler(...)
    local args = { ...};

    local content = args[2];
    local contentHashCode = LuaEntry.StableHash(content)
    if PopUpDlg._ContentHashCodeMap:Contain(contentHashCode) then 
        local dlgID = PopUpDlg._ContentHashCodeMap:Find(contentHashCode);
        if dlgID ~= nil then 
            return DlgMgr:GetDlgByID(dlgID); 
        end 
        return nil;
    end 

    local dlg = DlgMgr:CreateDlg(PopUpDlg);
    dlg._dlgArgs = PopUpArgs.Create(args);

    dlg._TitleText.text = dlg._dlgArgs._Title;
    dlg._ContentText.text = dlg._dlgArgs._Content;
    dlg._ContentText:AutoAlignment();
    local table = args[3];
    dlg._CommonHandlerCaller = dlg._dlgArgs._CommonHandlerCaller;
    dlg._CommonHandler = dlg._dlgArgs._CommonHandler;
    dlg._CommonHandlerArgs = dlg._dlgArgs._CommonHandlerArgs;
    if #table == 1 then
        dlg._OkBtn.visible = false;
        dlg._CancleBtn.visible = false;
        dlg._OkBigBtn.visible = true;
        dlg._OkBigBtn.text = dlg._dlgArgs._OkBigBtnText;
        dlg._OkBigBtn:SetClickHandler(dlg, dlg.OnBtnHandler, { dlg._dlgArgs._OkBigBtnID, dlg._dlgArgs._CommonHandlerArgs });
    else
        dlg._OkBtn.visible = true;
        dlg._CancleBtn.visible = true;
        dlg._OkBigBtn.visible = false;

        dlg._CancleBtn.text = dlg._dlgArgs._CancleBtnText;
        dlg._OkBtn.text = dlg._dlgArgs._OkBtnText;
        dlg._CancleBtn:SetClickHandler(dlg, dlg.OnBtnHandler, { dlg._dlgArgs._CancleBtnID, dlg._dlgArgs._CommonHandlerArgs });
        dlg._OkBtn:SetClickHandler(dlg, dlg.OnBtnHandler, { dlg._dlgArgs._OkBtnID, dlg._dlgArgs._CommonHandlerArgs });

    end
    if dlg._dlgArgs._CloseTime ~= nil then
        dlg._CloseTimer = dlg:AddTimer(PopUpDlg.ClosePopUpDlg, dlg._dlgArgs._CloseTime, 1);
    end
    dlg._ContentHashCode = contentHashCode;
    PopUpDlg._ContentHashCodeMap:Insert(dlg._ContentHashCode,dlg:GetID());
    return dlg;
end


function PopUpDlg:ClosePopUpDlg()
    self:Close();
    if self._dlgArgs._AutoCloseHandler ~= nil then 
        self._dlgArgs._AutoCloseHandler(self._dlgArgs._CommonHandlerCaller);
    end
end

function PopUpDlg:OnClose()
    if self._CloseTimer ~= nil then
        self:ClearTimer(self._CloseTimer);
        self._CloseTimer = nil;
    end
    if PopUpDlg._ContentHashCodeMap:Contain(self._ContentHashCode) then 
        PopUpDlg._ContentHashCodeMap:Remove(self._ContentHashCode);
    end
end

function PopUpDlg:OnBtnHandler(_, argsTable)
    self:Close();
    if self._CommonHandlerCaller ~= nil and self._CommonHandler ~= nil then
        if #argsTable == 1 then
            self._dlgArgs._CommonHandler(self._dlgArgs._CommonHandlerCaller, argsTable[1]);
        else
            self._dlgArgs._CommonHandler(self._dlgArgs._CommonHandlerCaller, argsTable[1], unpack(argsTable[2]));
        end
    end
end

function PopUpDlg:EnabledImage()
    self._bgImage.enabled = true;
end

-- 设置文字对齐方式
function PopUpDlg:SetLabelAlignment(alignment)
    self._ContentText.alignment = alignment;
end

function PopUpDlg:HideCloseBtn()
    self._CloseBtn.visible = false;
end 
-- endregion
